<template>
	<HyCard title="基本信息">
		<HyForm :hyFormDefault="hyFormDefault" :hyFormConfig="hyFormConfig" :hyFormFields="hyFormFields" ref="hyFormRef" />
	</HyCard>
</template>
<script setup lang="ts">
import HyForm from '/@/components/HyForm/index.vue';
import HyCard from '/@/components/HyCard/index.vue';
import { useDict, useTreeDict } from '/@/hooks/dict';
import { ElMessage } from 'element-plus';
const formType: string = inject('formType', 'submit');
// 定义字典
const { ethnic_group, expert_sources, gender } = useDict('ethnic_group', 'expert_sources', 'gender');
const { expert_type, political_landscape } = useTreeDict('expert_type', 'political_landscape');
//配置基本信息表单
const hyFormFields = computed(() => {
	return {
		expertInfoId: {
			type: 'input',
			hidden: true,
		},
		expertName: {
			label: '姓名',
			type: 'input',
			required: true,
		},
		expertTel: {
			label: '手机号码',
			type: 'input',
			required: true,
			maxlength: 11,
			rules: [
				{
					pattern: /^((0\d{2,3}-\d{7,8})|(1[3456789]\d{9}))$/,
					message: '请输入正确的手机号码格式',
					trigger: 'blur',
				},
			],
		},
		origin: {
			label: '籍贯',
			required: true,
			type: 'input',
		},
		ethnicGroup: {
			label: '民族',
			type: 'select',
			options: ethnic_group.value,
			required: true,
		},
		identityNumber: {
			label: '身份证号',
			type: 'input',
			required: true,
			rules: [
				{
					pattern: /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}(\d|x|X)$/,
					message: '身份证号码格式不正确',
					trigger: 'change',
				},
			],
			blur: (e: any) => IdBlur(e),
		},
		gender: {
			label: '性别',
			type: 'select',
			required: true,
			disabled: true,
			placeholder: '请输入身份证号码',
			options: gender.value,
		},
		birthDate: {
			label: '出生年月日',
			type: 'input',
			required: true,
			placeholder: '请输入身份证号码',
			disabled: true,
		},
		politicalLandscapeArr: {
			label: '政治面貌',
			required: true,
			type: 'cascader',
			options: political_landscape.value,
			showAllLevels: true,
		},
		officePhone: {
			label: '办公电话',
			type: 'input',
		},
		bankcardNumber: {
			label: '银行卡号',
			type: 'input',
			required: true,
			rules: [
				{
					pattern: /^\d+$/,
					message: '卡号输入错误',
					trigger: 'change',
				},
			],
		},
		accountBank: {
			label: '开户银行',
			type: 'input',
			required: true,
		},
		expertSources: {
			label: '专家来源',
			type: 'select',
			options: expert_sources.value,
			required: true,
			change: (value: string) => expertSourcesChange(value),
		},
		unitUnifiedSocialCreditcode: {
			label: '推荐单位统一社会信用代码',
			type: 'input',
			required: true,
			remove: removeOther.value,
		},
		unitName: {
			label: '推荐单位名称',
			type: 'input',
			required: true,
			remove: removeOther.value,
		},
		expertTypeArr: {
			label: '专家类别',
			type: 'cascader',
			options: expert_type.value,
			required: true,
			showAllLevels: true,
		},
		headlessPhoto: {
			type: 'input',
			hidden: true,
		},
		headlessPhotoFiles: {
			label: '一寸红底免冠照',
			required: true,
			type: 'upload',
			fileType: ['.jpg', '.jpeg', '.png'],
			fileSize: 200,
			fileLimit: 1,
		},
	};
});

const hyFormRef = ref();
//动态表单配置
const hyFormConfig = computed(() => {
	return {
		labelWidth: '120px',
		span: 8,
		offset: 0,
		type: formType,
	};
});
const props = defineProps({
	detailData: {
		type: Object,
		default: () => {
			return {};
		},
	},
});
const hyFormDefault = ref({});
watch(
	() => props.detailData,
	(nv1) => {
		hyFormDefault.value = nv1;
		nextTick(() => {
			expertSourcesChange(nv1.expertSources);
		});
	},
	{ immediate: true }
);
//专家来源
const removeOther = ref(true);
const expertSourcesChange = (value: string) => {
	if (value === '2') {
		removeOther.value = false;
	} else {
		removeOther.value = true;
	}
};
// 选中身份证时，自动回显生日,自动设置性别
const IdBlur = (value: any) => {
	const identityNum = value.target.value;
	if (identityNum) {
		const reg = /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}(\d|x|X)$/;
		if (reg.test(identityNum) === false) {
			ElMessage.error('身份证号输入有误，请重新输入');
		} else {
			//设置生日
			const birthYear = identityNum.slice(6, 10);
			const birthMonth = identityNum.slice(10, 12);
			const birthDay = identityNum.slice(12, 14);
			hyFormRef.value.hyForm.birthDate = `${birthYear}-${birthMonth}-${birthDay}`;
			//设置性别
			const sex = identityNum.slice(16, 17);
			if (sex % 2 === 0) {
				hyFormRef.value.hyForm.gender = '2';
			} else {
				hyFormRef.value.hyForm.gender = '1';
			}
		}
	}
};
const check = async () => {
	const result = await hyFormRef.value.check();
	if (result) return true;
	return false;
};
const getData = () => {
	return hyFormRef.value.hyForm;
};
defineExpose({ getData, check });
</script>
<style scoped lang="scss"></style>
